package com.iyjhsoc.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.iyjhsoc.game.dao.GameRoleDAO;
import com.iyjhsoc.game.domain.VO.GameRoleVO;
import com.iyjhsoc.game.domain.dto.GameRoleDTO;
import com.iyjhsoc.game.domain.entity.GameRoleDO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.GameRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GameRoleServiceImpl extends ServiceImpl<GameRoleDAO, GameRoleDO> implements GameRoleService {

    @Autowired
    GameRoleDAO roleDAO;

    @Override
    public Response saveGameRole(GameRoleDO gameRoleDO) {
        QueryWrapper<GameRoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",gameRoleDO.getName());
        GameRoleDO val = roleDAO.selectOne(queryWrapper);
        if(val != null){
            return Response.ERROR("英雄已经存在");
        }
        Response response = checkData(gameRoleDO);
        if(response.getCode() == 0){
            return response;
        }
        int insert = roleDAO.insert(gameRoleDO);
        if(insert == 1){
            return Response.SUCCESS("保存成功");
        }
        return Response.ERROR("保存失败");
    }

    @Override
    public Response queryList(GameRoleVO gameRoleVO) {
        PageHelper.startPage(gameRoleVO.getPageNum(),gameRoleVO.getPageSize());
        List<GameRoleDTO> gameRoleDTOS = roleDAO.queryList(gameRoleVO);
        return Response.SUCCESS(new PageInfo<>(gameRoleDTOS));
    }

    public Response checkData(GameRoleDO gameRoleDO){
        if(StringUtil.isEmpty(gameRoleDO.getAbb())){
            return Response.ERROR("简称不能为空");
        }
        if(StringUtil.isEmpty(gameRoleDO.getName())){
            return Response.ERROR("英雄名称不能为空");
        }
        if(StringUtil.isEmpty(gameRoleDO.getGameId())){
            return Response.ERROR("游戏Id不能为空");
        }
        return Response.SUCCESS();
    }
}
